<script>
import {formatTime} from "@/utils/formatTime.js";
import {all, add, edit, del,filter} from '@/api/comment.js'

export default {
  name: "comment",
  methods: {
    formatTime,
    refresh() {
      all().then(res => {
        if (res.code === 200) {
          this.commentData = res.data
        } else {
          this.$message.error("获取数据失败")
        }
      })
    },
    handleDelete(id) {
      del(id).then(res => {
        if (res.code === 200) {
          this.$message.success("删除成功")
          this.refresh()
        } else {
          this.$message.error("删除失败")
        }
      })
    },
    handleEdit(data) {
      this.editCommentDialogVisible = true;
      this.editCommentFormData = data;
    },
    editComment(data) {
      const req = {
        id: data.id,
        commentContent: data.commentContent,
        star: parseInt(data.star)
      }
      edit(req).then(res => {
        if (res.code === 200) {
          this.$message.success("修改成功")
          this.refresh()
          this.editCommentDialogVisible = false
          this.editCommentFormData = {}
        } else {
          this.$message.error("修改失败")
        }
      })
    },
    addComment(data) {
      add(data).then(res => {
        if (res.code === 200) {
          this.$message.success("添加成功")
          this.refresh()
          this.addCommentDialogVisible = false
          this.addCommentFormData = {}
        } else {
          this.$message.error(res.data)
        }
      })
    },
    filterComment(data){
      filter(data).then(res => {
        if (res.code === 200) {
          this.commentData = res.data
          this.filterCommentDialogVisible = false
        } else {
          this.$message.error("获取数据失败")
        }
      })
    }
  },
  data() {
    return {
      commentData: [],
      editCommentDialogVisible: false,
      addCommentDialogVisible: false,
      filterCommentDialogVisible: false,
      filterCommentFormData: {},
      editCommentFormData: {},
      addCommentFormData: {},
    }
  },
  created() {
    this.refresh()
  }
}
</script>

<template>
  <div id="comment-title">
    评论管理
  </div>
  <div id="comment-box">
    <div id="comment-form">
      <el-button type="primary" @click="filterCommentDialogVisible = true">增加检索</el-button>
      <el-button type="primary" @click="addCommentDialogVisible = true">新增数据</el-button>
      <el-button type="primary">导入数据</el-button>
      <el-button type="primary">导出数据</el-button>
      <el-button type="primary" @click="refresh">刷新</el-button>
    </div>
    <div>
      <el-table id="comment-data" :data="commentData">
        <el-table-column type="selection" width="50px"/>
        <el-table-column label="被评价商品名" prop="goodsName" width="150"></el-table-column>
        <el-table-column label="用户名" prop="userName" width="100"></el-table-column>
        <el-table-column label="评价内容" prop="commentContent" width="200"></el-table-column>
        <el-table-column label="订单号" prop="ordersNo" width="200"></el-table-column>
        <el-table-column label="用户头像" prop="avatarUrl" width="200">
          <template #default="scope">
            <img :src="scope.row.avatarUrl" style="width: 100px; height: 100px;" alt="图片">
          </template>
        </el-table-column>
        <el-table-column label="星级" prop="star" width="100"></el-table-column>
        <el-table-column label="创建时间" prop="createTime" width="200">
          <template #default="scope">
            <span>{{ formatTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="修改时间" prop="updateTime" width="200">
          <template #default="scope">
            <span>{{ formatTime(scope.row.updateTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="300">
          <template #default="scope">
            <el-button @click="handleEdit(scope.row)" type="primary">编辑</el-button>
            <el-button @click="handleDelete(scope.row.id)" type="danger">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
  <!-- 修改表单 -->
  <div>
    <el-dialog title="修改评论信息" v-model="editCommentDialogVisible">
      <el-form :model="editCommentFormData">
        <el-form-item label="被评价商品名">
          <el-input v-model="editCommentFormData.goodsName" disabled></el-input>
        </el-form-item>
        <el-form-item label="用户名">
          <el-input v-model="editCommentFormData.userName" disabled></el-input>
        </el-form-item>
        <el-form-item label="评价内容">
          <el-input v-model="editCommentFormData.commentContent"></el-input>
        </el-form-item>
        <el-form-item label="星级">
          <el-input v-model="editCommentFormData.star"></el-input>
        </el-form-item>
        <el-form-item label="订单号">
          <el-input v-model="editCommentFormData.ordersNo" disabled></el-input>
        </el-form-item>
        <el-form-item label="用户头像">
          <el-input v-model="editCommentFormData.avatarUrl" disabled></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="info" @click="editCommentDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="editComment(editCommentFormData)">提交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
  <!--添加表单-->
  <div>
    <el-dialog title="添加评论" v-model="addCommentDialogVisible" destroy-on-close>
      <el-form :model="addCommentFormData">
        <el-form-item label="被评价商品ID">
          <el-input v-model="addCommentFormData.goodsId"></el-input>
        </el-form-item>
        <el-form-item label="评价用户ID">
          <el-input v-model="addCommentFormData.userId"></el-input>
        </el-form-item>
        <el-form-item label="订单号">
          <el-input v-model="addCommentFormData.ordersId"></el-input>
        </el-form-item>
        <el-form-item label="评价等级">
          <el-select v-model="addCommentFormData.star" placeholder="请选择">
            <el-option label="1星" value="1"></el-option>
            <el-option label="2星" value="2"></el-option>
            <el-option label="3星" value="3"></el-option>
            <el-option label="4星" value="4"></el-option>
            <el-option label="5星" value="5"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="评价内容">
          <el-input v-model="addCommentFormData.commentContent"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="info" @click="addCommentDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="addComment(addCommentFormData)">提交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
<!-- 过滤 -->
  <div>
    <el-dialog title="条件筛选" v-model="filterCommentDialogVisible" :close-on-click-modal="true">
      <el-form :model="filterCommentFormData">
        <el-form-item label="商品名称">
          <el-input v-model="filterCommentFormData.goodsName"></el-input>
        </el-form-item>
        <el-form-item label="用户名">
          <el-input v-model="filterCommentFormData.userName"></el-input>
        </el-form-item>
        <el-form-item label="订单号">
          <el-input v-model="filterCommentFormData.ordersNo"></el-input>
        </el-form-item>
        <el-form-item label="星级">
          <el-select v-model="filterCommentFormData.star" placeholder="请选择">
            <el-option label="1星" value="1"></el-option>
            <el-option label="2星" value="2"></el-option>
            <el-option label="3星" value="3"></el-option>
            <el-option label="4星" value="4"></el-option>
            <el-option label="5星" value="5"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="info" @click="filterCommentDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="filterComment(filterCommentFormData)">提交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<style scoped>
#comment-box {
  background-color: #68a273;
  width: 90%;
  height: 90%;
  margin-top: 2%;
  margin-left: 3%;
  border-radius: 30px;
}

#comment-title {
  font-size: 30px;
  margin-left: 20px;
}

#comment-form {
  display: flex;
  padding-top: 40px;
  justify-content: flex-end;
  margin-right: 80px;
  margin-bottom: 10px;
}
</style>